#include "NeoCalendarHeaderDelegate.h"
#include <QAbstractItemView>
#include <QDate>
#include <QPainter>

NeoCalendarHeaderDelegate::NeoCalendarHeaderDelegate(QObject *parent)
    : QStyledItemDelegate(parent), mDisplayMode(NeoCalendarType::DisplayMode::DayView), mIsTransparent(false)
{
}

NeoCalendarHeaderDelegate::~NeoCalendarHeaderDelegate() = default;

void NeoCalendarHeaderDelegate::setDisplayMode(NeoCalendarType::DisplayMode mode)
{
    if (mDisplayMode != mode)
    {
        mDisplayMode = mode;
    }
}

void NeoCalendarHeaderDelegate::setTransparent(bool transparent)
{
    if (mIsTransparent != transparent)
    {
        mIsTransparent = transparent;
    }
}

void NeoCalendarHeaderDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (!mIsTransparent)
    {
        painter->save();
        painter->setPen(Qt::NoPen);
        painter->setBrush(Qt::NoBrush);
        painter->setRenderHint(QPainter::Antialiasing, true);
        painter->setRenderHint(QPainter::TextAntialiasing, true);
        painter->setPen(Qt::black);
        QRect rect = option.rect.adjusted(mSpace, mSpace, -mSpace, -mSpace);
        painter->drawText(rect, Qt::AlignCenter, index.data().toString());
        painter->restore();
    }
}

QSize NeoCalendarHeaderDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize size = qobject_cast<const QAbstractItemView *>(option.widget)->viewport()->size();
    switch (mDisplayMode)
    {
    case NeoCalendarType::DayView:
    {
        return QSize(size.width() / mDayShowColumnCount, size.height() / mDayShowRowCount);
    }
    case NeoCalendarType::MonthView:
    case NeoCalendarType::YearView:
    {
        return QSize();
    }
    default:
        break;
    }
    return QStyledItemDelegate::sizeHint(option, index);
}